package model
{
	import flash.data.SQLConnection;
	import flash.data.SQLStatement;
	import flash.events.SQLErrorEvent;
	import flash.events.SQLEvent;
	import flash.filesystem.File;
	import flash.filesystem.FileMode;
	import flash.filesystem.FileStream;
	
	import mx.controls.Alert;
	import mx.managers.CursorManager;
	
	public class Inventory
	{
		public static var sql_conn:SQLConnection;
		private var file_db:File = File.documentsDirectory.resolvePath("ndx/data.db");
		public function Inventory()
		{
			CursorManager.setBusyCursor();
			if (!file_db.exists) {
				var file_dir:File = File.documentsDirectory.resolvePath("ndx");	
				if (!file_dir.exists) file_dir.createDirectory();
				var fs_db:FileStream = new FileStream();
				fs_db.open(file_db, FileMode.WRITE);
				fs_db.close();
			}
			
			sql_conn = new SQLConnection();
			sql_conn.open(file_db);
			CursorManager.removeBusyCursor();
		}
		
		private function hdlr_failure(evt:SQLErrorEvent):void {
			Alert.show("SQL Connect failure: " + evt.toString());
		}
		
		public function hdlr_conn_success(evt:SQLEvent):void {
			
		}
		
		public function func_create_schemas():void {
			
		}

		
		/**
		 * create table
		*/
		public function func_create_table(str_create:String = "CREATE TABLE IF NOT EXISTS adjustments pk TEXT, product_pk TEXT"):void {
			CursorManager.setBusyCursor();
			var sql_statement:SQLStatement = new SQLStatement();
			sql_statement.sqlConnection = sql_conn;
			sql_statement.text = str_create;
			sql_statement.execute();
			CursorManager.removeBusyCursor();
		}

		
		/**
		 * create index
		*/
		public function func_create_index(str_create:String):void {
			CursorManager.setBusyCursor();
			var sql_statement:SQLStatement = new SQLStatement();
			sql_statement.sqlConnection = sql_conn;
			sql_statement.text = str_create;
			sql_statement.execute();
			CursorManager.removeBusyCursor();
		}
	}
}